// Copyright (c) 2010 Thomas Stuefe
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#pragma once

#include "ErrorCode.h"

using namespace System;

namespace zencat {
namespace sqlite {


	// forward declarations
	interface class IStatement;
	interface class IDatabase;



	/// <summary>
	/// Describes an SQLite error condition. 
	/// <see cref="SqliteErrCode"/> contains the sqlite error code (see http://www.sqlite.org/c3ref/c_abort.html),
	/// <see cref="SqliteErrMsg"/> contains the sqlite error message (see sqlite3_errmsg()), 
	/// <see cref="Database"/> is the reference to the database and <see cref="Statement"/> the associated statement.
	/// (Both <see cref="Database"/> and <see cref="Statement"/> may be null if there was no associated database
	/// or statement - e.g. if <see cref="Sqlite::OpenDatabase"/> fails).
	///
	/// Note: SqliteException will also be thrown for library-internal errors (errors not having to do with
	/// sqlite itself but with the zencat wrapper library). In those cases, <see cref="SqliteErrCode"/> will be
	/// <see cref="ErrorCode::EC_UNKNOWN"/> and <see cref="SqliteErrMsg"/> will be null.
	/// </summary>
	public ref class SqliteException sealed : public System::Exception {
		
	public:

		/// <summary>
		/// gets the raw numerical error code (sqlite3_extended_errcode())
		/// (see also: http://www.sqlite.org/c3ref/c_abort.html )
		/// Note: if the exception originated not from sqlite itself, but from the wrapper library,
		/// this property will be -1.
		/// </summary>
		property int SqliteRawErrCode {
			int get() {  return _sqliteRawErrCode; }
		}
		
		/// <summary>
		/// Gets the sqlite error code (sqlite3_extended_errcode())
		/// (see also: http://www.sqlite.org/c3ref/c_abort.html ).
		/// Note: if the exception originated not from sqlite itself, but from the wrapper library,
		/// this property will be <see cref="SqliteErrCode.EC_UNKNOWN"/>.
		/// </summary>
		property ErrorCode SqliteErrCode {
			ErrorCode get() {  return _sqliteErrCode; }
		}

		/// <summary>
		/// Gets the sqlite error message, if there was one (sqlite3_errmsg())
		/// (see also: http://www.sqlite.org/c3ref/c_abort.html )
		/// Note: if the exception originated not from sqlite itself, but from the wrapper library,
		/// this property will be null.
		/// </summary>
		property String^ SqliteErrMsg {
			String^ get() {  return _sqliteErrMsg; }
		}

		/// <summary>
		/// Gets the associated <see cref="IDatabase"/> database.
		/// Will be null if there is no associated database object (e.g. if a database failed to open).
		/// </summary>
		property IDatabase^ Database {
			IDatabase^ get() { return _db; }
		}

		/// <summary>
		/// Gets the associated <see cref="IPreparedStatement"/> statement, if error was caused 
		/// by one.
		/// Will be null if there is no associated statement object.
		/// </summary>
		property IStatement^ Statement {
			IStatement^ get() { return _stmt; }
		}

	internal:

		SqliteException(IDatabase^ db, IStatement^ stmt, int sqliteRawErrCode, const wchar_t* sqliteErrMsg, const wchar_t* context);

	private:

		// the raw error code
		int _sqliteRawErrCode;

		// parsed error code
		ErrorCode _sqliteErrCode;

		// sqlite error message (sqlite3_err_msg())
		String^ _sqliteErrMsg;

		// associated database
		IDatabase^ _db;

		// associated statement (if error happened in the context of one)
		IStatement^ _stmt;

	}; // end: SqliteException



} // end namespace sqlite

} // end namespace zencat